package com.teacher.controller;

import com.teacher.entity.LearningPerformance;
import com.teacher.service.LearningPerformanceService;
import com.teacher.untils.ResultEntity;
import com.teacher.untils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "学生学习表现管理")
@RequestMapping("/learningPerformance")
@RestController
public class LearningPerformanceController {
    @Autowired
    private LearningPerformanceService learningPerformanceService;

    // 批量导入教师评语
    @ApiOperation("学生学习表现批量导入")
    @RequestMapping(value = "/excelImport/learningPerformance", method = RequestMethod.POST)
    public ResultEntity uploadCommentsOfExcel(HttpServletRequest servletRequest){
        MultipartFile file = null;
        if (servletRequest instanceof StandardMultipartHttpServletRequest) {
            MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
            List<MultipartFile> fileList = multipartRequest.getFiles("file");
            if (null != fileList)
                file = fileList.get(0);
        }
        if(file.isEmpty()) return ResultUtils.build("文件不能为空");
        String academicYear = servletRequest.getParameter("academicYear");
        return ResultUtils.build(learningPerformanceService.uploadCommentsOfExcel(file, academicYear));
    }

    // 增加
    @ApiOperation("学生学习表现增加")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultEntity  addLearningPerformance(@RequestBody LearningPerformance LearningPerformance){
        return ResultUtils.build(learningPerformanceService.addLearningPerformance(LearningPerformance));
    }

    // 根据id查看详情
    @ApiOperation("学生学习表现查看详情")
    @GetMapping(value = "/getDetailsById/{id}")
    public ResultEntity getDetailsById(@PathVariable("id") String id){
        return ResultUtils.build(learningPerformanceService.getDetailsById(id));
    }

    // 根据id删除
    @ApiOperation("学生学习表现删除")
    @DeleteMapping("/delete/{id}")
    public void deleteLearningPerformance(@PathVariable("id") String id){
        learningPerformanceService.deleteLearningPerformance(id);
    }

    // 根据id批量删除
    @ApiOperation("学生学习表现批量删除")
    @DeleteMapping("/batch")
    public void deleteLearningPerformanceList(String idList){
        learningPerformanceService.deleteLearningPerformanceList(idList);
    }

    // 根据条件分页查询
    @ApiOperation(value = "根据条件分页查询",notes = "根据条件分页查询")
    @GetMapping("/specificPage")
    public ResultEntity specificPageQueryLearningPerformanceList(LearningPerformance LearningPerformance,int page,int size){
        return ResultUtils.build(learningPerformanceService.specificPageQueryLearningPerformanceList(LearningPerformance,page,size));
    }
}
